package com.ssyu.examination.questionbank.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.ssyu.examination.conmmon.PageModel;
import com.ssyu.examination.conmmon.util.DBUtil;
import com.ssyu.examination.questionbank.entity.QuestionQesEntity;

public class QuestionQesService {

	public static PageModel<QuestionQesEntity> listQuestion(int page, int rows,
			String question, int questionType, int courseId, int difficulty, int state) {
		List<Object> objects = new ArrayList<Object>();

		StringBuilder sb = new StringBuilder();
		sb.append("SELECT sq.id,sq.question,sq.question_type,sc.text AS course_name,sq.difficulty,sq.state FROM qes_qestions sq,sys_course sc ");
		sb.append("WHERE sq.course_id=sc.id ");
		
		if (questionType != 0) {
			sb.append("AND sq.question_type=? ");
			objects.add(questionType);
		}
		
		if (difficulty != 0) {
			sb.append("AND sq.difficulty=? ");
			objects.add(difficulty);
		}
		
		sb.append("AND sq.state=? ");
		objects.add(state);
		if (courseId >1 ) {
			sb.append("AND sq.course_id=? ");
			objects.add(courseId);
		}
		if (StringUtils.isNotBlank(question)) {
			sb.append("AND sq.`question` like ?");
			objects.add("%" + question + "%");
		}
		sb.append(" ORDER BY sq.id DESC LIMIT ?,?");
		objects.add((page - 1) * rows);
		objects.add(rows);
		return DBUtil.executeQueryByPage(QuestionQesEntity.class, sb.toString(),
				objects);
	}

}
